﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;

namespace Gameplay.PVE.Survivor
{
    public class NormalAttackSkill: SkillBase
    {
        public int ammoCount;
        private int loopCount;
        public EBulletDamageType damageType = EBulletDamageType.NormalAttack;
        public int groupSkillId;

        /*protected override bool NeedStop()
        {
            if (ammoCount < unit.consumedAmmoCount)
            {
                return true;
            }
            return base.NeedStop();
        }*/

        protected override SkillBase GetExtraSkill()
        {
            return unit.GetExtraChildSkill(groupSkillId);
        }

        protected override void CastBullet(int bulletId,int pointIndex,int bulletIndex)
        {
            /*if (unit.unitId == 1000)
            {
                BattleDebug.LogError("NormalAttackSkill CastBullet " + target.unitId);
            }*/
            if (target == null || target.isDead)
            {
                return;
            }
            base.CastBullet(bulletId,pointIndex,bulletIndex);
            unit.consumedAmmoCount++;
        }

        protected override Transform GetBulletCreateEffectParent()
        {
            return unit.gunPoint;
        }

        protected override Vector3 GetBulletStartForward(int index)
        {
            return unit.target.AimPoint.position - GetBulletStartPosition();
        }

        protected override void FillTarget(SkillTarget target,bool useParent = false)
        {
            if (target.targetType == ESkillTargetType.Unit)
            {
                target.unit = unit.target;
                target.OnGetTarget();
                target.isValid = target.unit != null;
                return;
            }
            base.FillTarget(target);
        }

        protected override void UpdateCast()
        {
            base.UpdateCast();
            if (target != unit.target)
            {
                SetTarget(unit.target);
            }
            if (config.reload_time > 0 && ammoCount < unit.consumedAmmoCount)
            {
                SwitchState(ESkillState.Recover);
            }
            else
            {
                if (TimeManager.time - stateStartTime >= castTime)
                {
                    loopCount++;
                    if (loopCount >= config.shoot_bullet_count)
                    {
                        SwitchState(ESkillState.Recover);
                    }
                    else
                    {
                        if (unit.team == 0)
                        {
                            unit.SearchTarget();
                            if (unit.target != null && NeedAim())
                            {
                                unit.StartAim();
                            }
                        }
                        SwitchState(ESkillState.Cast);
                        AutoSearch();
                        StartCast();
                    }
                }
            }
        }

        protected override void OnStart()
        {
            base.OnStart();
            unit.TargetForward = target.Position - unit.position;
            loopCount = 0;
            if (unit.team == 2)
            {
                unit.StopMove();
            }
        }
    }
}
